using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class TreeViewPicker : Form
    {
        private ProjectTree _tree = null;
        private TreeNodeTag _tag = null;
        private bool _pickHelpItemOnly = false;

        public bool PickHelpItemOnly
        {
            get { return _pickHelpItemOnly; }
            set { _pickHelpItemOnly = value; }
        }

        public TreeNodeTag SelectedTreeNodeTag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        public TreeViewPicker()
            : this(null)
        { }

        public TreeViewPicker(object filterObjectType) //ie, if this is set to HCContext, then only context object will be shown
        {
            InitializeComponent();
            _tree = new ProjectTree();
            _tree.SelectionOnly = true;
            _tree.Width = panelMain.Width;
            _tree.Height = panelMain.Height;
            _tree.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
            panelMain.Controls.Clear();
            panelMain.Controls.Add(_tree);

            foreach (TreeNode rootLevelNode in MainForm.projectTree.Tree.Nodes)
            {
                TreeNode newNode = (TreeNode)rootLevelNode.Clone();
                _tree._manifestNode = newNode;
                //clear root node contenxt menu
                if (newNode.ContextMenu != null)
                    newNode.ContextMenu.MenuItems.Clear();
                
                List<TreeNode> nodesToKeep = new List<TreeNode>();
                foreach (TreeNode secondLevelNode in newNode.Nodes)
                {
                    //clear context menu for secondary nodes (help item level)
                    if (secondLevelNode.ContextMenu != null)
                        secondLevelNode.ContextMenu.MenuItems.Clear();

                    if (filterObjectType != null)
                    {
                        if (secondLevelNode.Tag != null)
                        {
                            TreeNodeTag tag = (TreeNodeTag)secondLevelNode.Tag;
                            if (tag.MirrorObject.GetType() == filterObjectType.GetType())
                            {
                                nodesToKeep.Add(secondLevelNode);
                            }
                        }
                    }
                }

                if (filterObjectType != null)
                {
                    newNode.Nodes.Clear();
                    foreach (TreeNode nodeToKeep in nodesToKeep)
                        newNode.Nodes.Add(nodeToKeep);
                }
                
                newNode.ExpandAll();
                _tree.Tree.Nodes.Add(newNode);
            }
            _tree.EnableDisableSearch();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            TreeNodeTag tag = _tree.SelectedTreeNodeTag;
            if (tag == null)
            {
                MessageBox.Show("Please select a help item or cancel");
                return;
            }

            if (!(tag.MirrorObject is HelpItem))
            {
                MessageBox.Show("The selected node must be a help item");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.SelectedTreeNodeTag = _tree.SelectedTreeNodeTag;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}